import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// 菜单状态管理
export default ({
    state: {
        //菜单列表
        menuList: [],
        //权限列表
        permList: [],
        //校验当前菜单是否已有路由
        hasRoute: false,
        //头部默认选中标签为 Index
        editableTabsValue: 'Index',
        //主页头部动态菜单标签
        editableTabs: [{
            title: '首页',
            name: 'Index',
        }]
    },
    mutations: {
        //写入菜单列表方法
        setMenuList: (state, menus) => {
            state.menuList = menus
        },
        //写入用户操作权限方法
        setPermList: (state, perms) => {
            state.permList = perms
        },
        //判断当前菜单是否已有路由
        checkRouteStatus: (state, hasRoute) => {
            state.hasRoute = hasRoute
            //sessionStorage - 针对一个 session 的数据存储（关闭窗口，存储的数据清空）
            // sessionStorage.setItem('hasRoute', hasRoute)
        },
        //添加头部标签方法
        addTab(state, tab) {

            let index = state.editableTabs.findIndex(e => e.name === tab.name)

            if (index === -1) {
                state.editableTabs.push({
                    title: tab.title,
                    name: tab.name,
                });
            }

            state.editableTabsValue = tab.name;
        },
        //用户退出清空数据
        resetState: (state) => {
            state.menuList = []
            state.permList = []

            state.hasRoutes = false
            state.editableTabsValue = 'Index'
            state.editableTabs = [{
                title: '首页',
                name: 'Index',
            }]
        },
    },
    actions: {}
})
